package com.htgd.gardenexpo.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.dao.GeBaseDictDao;
import com.htgd.gardenexpo.dao.GeBaseDictItemDao;
import com.htgd.gardenexpo.dao.GeConPlantImgMapper;
import com.htgd.gardenexpo.entity.GeBaseDict;
import com.htgd.gardenexpo.entity.GeBaseDictItem;
import com.htgd.gardenexpo.entity.GeConPlant;
import com.htgd.gardenexpo.dao.GeConPlantMapper;
import com.htgd.gardenexpo.entity.GeConPlantImg;
import com.htgd.gardenexpo.page.PlantQuery;
import com.htgd.gardenexpo.service.GeBaseDictService;
import com.htgd.gardenexpo.service.GeConPlantService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.htgd.gardenexpo.vo.GeConPlantVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 植物百科 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-08-23
 */
@Service
public class GeConPlantServiceImpl extends ServiceImpl<GeConPlantMapper, GeConPlant> implements GeConPlantService {

    @Autowired
    private GeConPlantMapper geConPlantMapper;

    @Autowired
    private GeBaseDictService geBaseDictService;

    @Autowired
    private GeConPlantImgMapper geConPlantImgMapper;


    @Override
    public PageInfo<GeConPlantVO> pageList(PlantQuery plantQuery) {
        PageHelper.startPage(plantQuery.getPageNum(), plantQuery.getPageSize());
        List<GeConPlantVO> list= geConPlantMapper.pageList(plantQuery);
        for (GeConPlantVO geConPlantVO : list) {
            String statusText = geBaseDictService.getStatusText(geConPlantVO.getStatus());
            geConPlantVO.setStatusText(statusText);
            //图片
            List<String> images = new ArrayList<>();
            QueryWrapper<GeConPlantImg> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("botany_id", geConPlantVO.getId());
            List<GeConPlantImg> geConPlantImgs = geConPlantImgMapper.selectList(queryWrapper);
            for (GeConPlantImg geConPlantImg : geConPlantImgs) {
                images.add(geConPlantImg.getFileName());
            }
            geConPlantVO.setImages(images);
        }
        return new PageInfo<>(list);
    }

    @Override
    public List<GeConPlantVO> plantList() {
        List<GeConPlantVO> list= geConPlantMapper.plantList();
//        for (GeConPlantVO geConPlantVO : list) {
//            String statusText = geBaseDictService.getStatusText(geConPlantVO.getStatus());
//            geConPlantVO.setStatusText(statusText);
//            //图片
//            List<String> images = new ArrayList<>();
//            QueryWrapper<GeConPlantImg> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("botany_id", geConPlantVO.getId());
//            List<GeConPlantImg> geConPlantImgs = geConPlantImgMapper.selectList(queryWrapper);
//            for (GeConPlantImg geConPlantImg : geConPlantImgs) {
//                images.add(geConPlantImg.getFileName());
//            }
//            geConPlantVO.setImages(images);
//        }
        return list;
    }
}
